package com.anmi.alfred.service.concreteservice;

import com.anmi.alfred.service.baseservice.SearchService;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Service;

import java.util.List;

// This class for testing! don't use!
@Service
public class OrderSearchService extends SearchService {

  public <T> List<T> orderTermQuery(String indexName, int from, int size, Class<T> clazz) {
    return matchAll(indexName, from, size, clazz);
  }

  /**
   * 这里是模糊匹配，但是不推荐，性能不佳，要求field 为 not analysed
   *
   * @param indexName
   * @param from
   * @param size
   * @param param
   * @return
   */
  public List<String> orderTermQuery(String indexName, int from, int size, String param) {
    SearchRequest searchRequest = new SearchRequest(indexName);
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    boolQueryBuilder.must(QueryBuilders.wildcardQuery("orderNum.keyword", "*" + param + "*"));
    searchSourceBuilder.query(boolQueryBuilder);
    searchRequest.source(searchSourceBuilder);
    return resolveSearchResponse(search(searchRequest));
  }
}
